<?php $this->headScript()
->appendFile($this->js('plugins/jquery.form.js'))
->appendFile($this->js('plugins/jquery.blockUI.js'))
?>
<script>
$(function(){

	 // unblock when ajax activity stops
    $(document).ajaxStop($.unblockUI);
    // 儲存檔案
    $("#saveBtn").click(function(){
        if ($("#chooseSn").val()=='')
            return false;
        $.blockUI({ message: '<h1><img src="<?php echo $this->BaseUrl()?>/images/loading.gif" /> <?php echo $this->translate("Just a moment..")?>.</h1>' });
        $("#setForm input[name='subjectSn']").attr('value',$("#chooseSn").val());
        $("#setForm input[name='yearSemester']").attr('value',$("#yearSemester").val());
        $("#setForm input[name='classSn']").attr('value',$("#classSn").val());
        $("#setForm input[name='studentSn']").attr('value',$("#chooseSn").val());
        $("#setForm").submit();
     });

    $("#yearSemester").change(function(){
//        $("#classSn").val('');
//        $("#chooseSn").val('');
        $("#selectForm").submit();
     });
    $("#classSn").change(function(){
//    	$("#chooseSn").val('');
        $("#selectForm").submit();
     });

    $("#chooseSn").change(function(){
    	$("#selectForm").submit();
     });

    $("#selectForm input[type='radio']").click(function(){
    	$("#chooseSn").val('');
    	$("#selectForm").submit();
      });

    // 輸入處理
    $(".description").keydown(function(e){
        var tabIndex =  parseInt($(this).attr('tabindex'));
        switch(e.which){
        case 9:
        case 38:
        	 tabIndex --;
        	 break;
        case 13:
        case 40:
            tabIndex ++;
            break;
        }
        if ($(".description[tabindex="+tabIndex+"]").length) {
            $(".description[tabindex="+tabIndex+"]").focus();
        }
    });


    $(".score").keydown(function(e){
      var tabIndex =  parseInt($(this).attr('tabindex'));

        switch (e.which) {
      case 9 : //tab
          var score = parseFloat($(this).attr('value'));
          if (isNaN(score)) score = -1;
          if (score < 0 || score > 100) {
              $(this).attr('value','');
              tabIndex --;
          }
          break;
      case 13 : // return
          var score = parseFloat($(this).attr('value'));
          if (isNaN(score)) score = -1;
          if (score < 0 || score > 100) {
              alert($(this).attr('value')+' <?php echo $this->translate('value error')?>');
              $(this).attr('value','');
          }
          else
              tabIndex++;
          break;
      case 40 :
          tabIndex ++;
       break;
      case 38 :
          tabIndex --;
       break;
      }
      if ($(".score[tabindex="+tabIndex+"]").length) {
          $(".score[tabindex="+tabIndex+"]").focus();
      }
   });

    $("#score-list table tbody tr:even").addClass('even');
    $("#score-list table tbody tr:odd").addClass('odd');
    // 檢查不及格
    checkFail();
});

function checkFail()
{
    $.each($("#score-list .score"),function(){
        $(this).removeClass('failed');
        var s = $(this).attr('value');
        if (s !='' && s < 60)
              $(this).addClass('failed');
     });
}
</script>

<style>
.not-register {color:red}
.sex-M {color: blue}
.sex-F {color: #e33}
.failed {color:red}
.focus {background:#ff0}
</style>
<div class="ui-widget">
<form action="" id="selectForm" method="post">
<select name="yearSemester"  id="yearSemester">
<?php echo $this->YearSemesterOptions($this->yearSemester,array('order'=>'year DESC'))?>
</select>
<select name="classSn" id="classSn">
<option value=""><?php echo $this->translate('choose class')?></option>
<?php echo $this->ClassNameOptions($this->yearSemester, $this->classSn)?>
</select>
<?php if ($this->classSn):?>
<?php if ($this->inputKind == 1):?>
<select id="chooseSn" name="subjectSn">
<option value=""><?php echo $this->translate('choose subject')?></option>
<?php foreach($this->subjectData as $subjectSn=>$val):?>
<option value="<?php echo $subjectSn?>" <?php Sfs_Html::printSelected($subjectSn,$this->subjectSn)?>><?php echo $val['name']?></option>
<?php endforeach?>
</select>
<?php else:?>
<select id="chooseSn" name="studentSn">
<option value=""><?php echo $this->translate('choose student')?></option>
<?php foreach($this->studentData as $studentSn=>$val):?>
<option class="sex-<?php echo $val['sex']?>" value="<?php echo $studentSn?>" <?php Sfs_Html::printSelected($studentSn,$this->studentSn)?>>
<?php echo $val['number'].' - '.$val['name']?><?php if ($val['study_condition'] > 0 ):?> (不在籍)<?php endif?></option>
<?php endforeach?>
</select>
<?php endif?>

<input type="button" id="saveBtn" value="<?php echo $this->translate('save score')?>" />
<?php foreach($this->inputKindArr as $key=>$val):?>
<input type="radio" name="input_kind" id="input_kind_<?php echo $key?>" value="<?php echo $key?>"
 <?php if ($this->inputKind == $key) Sfs_Html::printChecked($key)?> />
<label for="input_kind_<?php echo $key?>"><?php echo $this->translate($val)?></label>
<?php endforeach;?>

<?php endif?>
</form>
<?php if ($this->classSn):?>
<div class="ui-content ui-corner-all"  id="score-list">
<?php
if ($this->inputKind == 1)
echo $this->render('semester-input/subject.phtml');
else
echo $this->render('semester-input/student.phtml');
?>
</div>
<?php endif?>
</div>